﻿// Chronometer  - 电子钟室外温度表
#include "Chronometer.h"

// 基准参数
const double DIAL_RADIUS_BASE = 180.0;
const double CENTER_X_BASE = 400.0;
const double CENTER_Y_BASE = 300.0;

// 每个数字对应的段状态（0-9）
const int digit_segments[10][7] = {
        {1,1,1,1,1,1,0}, // 0
        {0,1,1,0,0,0,0}, // 1
        {1,1,0,1,1,0,1}, // 2
        {1,1,1,1,0,0,1}, // 3
        {0,1,1,0,0,1,1}, // 4
        {1,0,1,1,0,1,1}, // 5
        {1,0,1,1,1,1,1}, // 6
        {1,1,1,0,0,0,0}, // 7
        {1,1,1,1,1,1,1}, // 8
        {1,1,1,1,0,1,1}  // 9
};
const int segments[7][4] = {
        {5, 0, 25, 0},     // 段a（顶部横线）
        {25, 0, 25, 20},   // 段b（右上竖线）
        {25, 25, 25, 45},  // 段c（右下竖线）
        {5, 45, 25, 45},   // 段d（底部横线）
        {0, 25, 0, 45},    // 段e（左下竖线）
        {0, 0, 0, 20},     // 段f（左上竖线）
        {5, 23, 25, 23}    // 段g（中间横线）
};

void drawCNM(float CENTER_X_BASE, float CENTER_Y_BASE, float  DIAL_RADIUS_BASE) {
    double dialRadius = DIAL_RADIUS_BASE;
    double centerX = CENTER_X_BASE;
    double centerY = CENTER_Y_BASE;
    double scale = dialRadius / 180;
    /*double dialRadius = 200;
    double centerX = 290;
    double centerY = 330;
    float scale = 0.9;*/
    //    initgraph(800, 600);
    setrendermode(RENDER_MANUAL);
    //    setbkcolor(EGERGB(97, 97, 97));
    ege_enable_aa(TRUE);  // 启用抗锯齿

    //    while (1) {
    outerCircle1(centerX, centerY, dialRadius);
    anniu(centerX, centerY, dialRadius);
    drawrectangle(centerX, centerY, dialRadius);
    drawnumbers(centerX, centerY, dialRadius);
    shadow(centerX, centerY, dialRadius);

    int h, m, s;
    get_time(&h, &m, &s);

    // 在main循环中修改调用方式：

    // 绘制小时、分钟、秒（每个数字宽30，高50）
    //draw_digit(0.5,379, 290, h / 10, EGERGB(42, 42, 42));    // 小时十位
    //draw_digit(0.5,422, 290, h % 10, EGERGB(42, 42, 42));    // 小时个位
    //draw_digit(0.5,467, 290, m / 10, EGERGB(42, 42, 42));   // 分钟十位
    //draw_digit(0.5,506, 290, m % 10, EGERGB(42, 42, 42));   // 分钟个位


    // 修改后的数字绘制调用（注意第一个参数改为scale）
    draw_digit(scale, centerX - 20 * scale, centerY - 10 * scale, h / 10, EGERGB(42, 42, 42));
    draw_digit(scale, centerX + 22 * scale, centerY - 10 * scale, h % 10, EGERGB(42, 42, 42));
    draw_digit(scale, centerX + 67 * scale, centerY - 10 * scale, m / 10, EGERGB(42, 42, 42));
    draw_digit(scale, centerX + 106 * scale, centerY - 10 * scale, m % 10, EGERGB(42, 42, 42));
    // 绘制冒号（闪烁效果）
    static int blink = 1;
    if (blink) {
        setcolor(EGERGB(42, 42, 42));
        circle(centerX + 60 * scale, centerY - 1 * scale, 1.5 * scale);
        circle(centerX + 60 * scale, centerY + 29 * scale, 1.5 * scale);
    }

    blink = blink;

    // 双缓冲刷新
//        delay_ms(500);  // 0.5秒刷新一次（冒号闪烁更流畅）
//        cleardevice();   // 清屏重新绘制
//    }
//    getch();
//    closegraph();
//    return 0;
}
// 修改后的draw_digit函数
void draw_digit(float scale, int x, int y, int num, int color) { // 第一个参数改为scale
    ege_enable_aa(TRUE);
    setlinewidth(5 * scale);  // 线宽按比例缩放
    for (int i = 0; i < 7; i++) {
        if (digit_segments[num][i]) {
            setcolor(color);
            // 所有坐标需要乘以scale
            ege_line(x + segments[i][0] * scale,
                y + segments[i][1] * scale,
                x + segments[i][2] * scale,
                y + segments[i][3] * scale);
        }
    }
}
//    // 绘制单个数字（x,y为左上角坐标，num为数字，color为颜色）
//void draw_digit(float s,int x, int y, int num, int color) {
//        ege_enable_aa(TRUE);  // 启用抗锯齿
//        setlinewidth(5);
//        for (int i = 0; i < 7; i++) {
//            if (digit_segments[num][i]) {
//                setcolor(color);
//                ege_line(x + segments[i][0]*s, y + segments[i][1]*s,
//                    x + segments[i][2]*s, y + segments[i][3]*s);
//            }
//        }
//    }


void get_time(int* h, int* m, int* s) {
    time_t now = time(NULL);
    struct tm* t = localtime(&now);
    *h = t->tm_hour;
    *m = t->tm_min;
    *s = t->tm_sec;
}

// 模拟数据输入
double updateinput1(double current_heading) {
    static int direction = 1;
    current_heading += direction * 1.5;
    // if (current_heading >= 360) direction = -1;
    // if (current_heading <= 0) direction = 1;
    return current_heading;
}

// 圆盘外框
void outerCircle1(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setlinewidth(3 * scale);
    setcolor(EGERGB(30, 30, 30));
    ege_circle(centerX, centerY, dialRadius + 21 * scale);
    setfillcolor(EGERGB(54, 50, 48));
    ege_fillcircle(centerX, centerY, dialRadius + 20 * scale);
}

// 数字和文字
void drawnumbers(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setcolor(EGERGB(210, 210, 210));// 刻度颜色
    setfont(20 * scale, 10 * scale, "MS UI Gothic");
    setbkmode(TRANSPARENT);// 去除文字背景色

    outtextxy(centerX - 115 * scale, centerY + 98 * scale, "SELECT");
    outtextxy(centerX + 31 * scale, centerY + 98 * scale, "CONTROL");
    outtextxy(centerX - 101 * scale, centerY - 150 * scale, "O.A.T.");
    outtextxy(centerX + 34 * scale, centerY - 149 * scale, "VOLTS");
    setfont(16 * scale, 8 * scale, "MS UI Gothic");
    outtextxy(centerX - 44 * scale, centerY + 169 * scale, "DAVTRON");
    setcolor(EGERGB(41, 41, 41));// 刻度颜色
    setfont(34 * scale, 17 * scale, "Cascadia Mono SemiBold");
    outtextxy(centerX - 129 * scale, centerY - 18 * scale, "UT");
    outtextxy(centerX - 90 * scale, centerY - 18 * scale, "LT");
    outtextxy(centerX - 127 * scale, centerY + 18 * scale, "FT");
    outtextxy(centerX - 88 * scale, centerY + 18 * scale, "ET");
    setfont(78 * scale, 40 * scale, "OCR A Extended");
    outtextxy(centerX - 35 * scale, centerY - 104 * scale, "2");
    setlinewidth(5 * scale);
    // 4
    ege_line(centerX + 7 * scale, centerY - 90 * scale, centerX + 7 * scale, centerY - 64 * scale);
    ege_line(centerX + 7 * scale, centerY - 65 * scale, centerX + 30 * scale, centerY - 65 * scale);
    ege_line(centerX + 31 * scale, centerY - 90 * scale, centerX + 31 * scale, centerY - 39 * scale);
    // E
    ege_line(centerX + 60 * scale, centerY - 88 * scale, centerX + 87 * scale, centerY - 88 * scale);// 1
    ege_line(centerX + 60 * scale, centerY - 64 * scale, centerX + 87 * scale, centerY - 64 * scale);// 2
    ege_line(centerX + 60 * scale, centerY - 41 * scale, centerX + 87 * scale, centerY - 41 * scale);// 3
    ege_line(centerX + 60 * scale, centerY - 88 * scale, centerX + 60 * scale, centerY - 41 * scale);// 竖
}

void shadow(double centerX, double centerY, double dialRadius)
{
    // 使用float保证计算精度（根据需求可改为double）
    const float DIAL_RADIUS_F = (float)dialRadius;
    const float CENTER_X_F = (float)centerX;
    const float CENTER_Y_F = (float)centerY;
    // 参数化定义（精确到小数点后6位）
    const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
    const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
    const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
    const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
    const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
    const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
    const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
    const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
    const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
    const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
    const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

    ege_enable_aa(TRUE);

    // 绘制函数（使用浮点坐标）
    auto draw_screw = [&](float cx, float cy) {
        // 精确到小数点后6位的渐变参数
        ege_setpattern_ellipsegradient(
            ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
            EGEACOLOR(0xFF, BLACK),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(ELLIPSE_SIZE),
            roundf(ELLIPSE_SIZE),
            EGEACOLOR(0, WHITE)
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 灰色渐变层（精确尺寸调整）
        const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
        ege_setpattern_ellipsegradient(
            ege_point{
                    roundf(cx - SCREW_OFFSET * 0.5f),
                    roundf(cy - SCREW_OFFSET * 0.5f)
            },
            EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(adjusted_size),
            roundf(adjusted_size),
            EGEACOLOR(0xFF, EGERGB(20, 20, 20))
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 十字线（亚像素级精度）
        setcolor(EGERGB(30, 30, 30));
        setlinewidth(roundf(LINE_WIDTH));

        const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
        ege_line(
            roundf(cx - CROSS_OFFSET1), roundf(cross_y),
            roundf(cx + CROSS_OFFSET1), roundf(cross_y)
        );

        const float cross_x = cx;
        const float cross_y_start = cy + CROSS_OFFSET2 / 4;
        const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
        ege_line(
            roundf(cross_x), roundf(cross_y_start),
            roundf(cross_x), roundf(cross_y_end)
        );
        };

    // 计算四个螺丝中心坐标（浮点运算）
    const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

    // 左上
    draw_screw(
        CENTER_X_F - screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右上
    draw_screw(
        CENTER_X_F + screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右下
    draw_screw(
        CENTER_X_F + screw_radius,
        CENTER_Y_F + DIAL_RADIUS_F
    );

    // 左下（新增）
    draw_screw(
        CENTER_X_F - screw_radius,
        CENTER_Y_F + DIAL_RADIUS_F
    );
}



// 绘制按钮
void anniu(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setfillcolor(EGERGB(85, 82, 81));
    // 按钮投影左
    for (double i = 0; i < 7; i += 0.2) {
        ege_fillcircle(centerX - 82 * scale + i * scale, centerY + 150 * scale - i * scale, 23 * scale);
        ege_fillcircle(centerX + 75 * scale + i * scale, centerY + 150 * scale - i * scale, 23 * scale);
        ege_fillcircle(centerX - 8 * scale + i * scale, centerY - 171 * scale - i * scale, 23 * scale);
    }
    // 按钮
    setfillcolor(EGERGB(143, 137, 135));
    ege_fillcircle(centerX - 82 * scale, centerY + 150 * scale, 20 * scale);
    ege_fillcircle(centerX + 75 * scale, centerY + 150 * scale, 20 * scale);
    ege_fillcircle(centerX - 8 * scale, centerY - 171 * scale, 20 * scale);
    setlinewidth(3 * scale);
    setcolor(EGERGB(158, 158, 158));
    ege_circle(centerX - 82 * scale, centerY + 150 * scale, 20 * scale);
    ege_circle(centerX + 75 * scale, centerY + 150 * scale, 20 * scale);
    ege_circle(centerX - 8 * scale, centerY - 171 * scale, 20 * scale);
}

// 圆角矩形框
void drawrectangle(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setfillcolor(EGERGB(33, 31, 29));
    ege_fillroundrect(centerX - 148 * scale, centerY - 111 * scale, 300 * scale, 84 * scale, 20 * scale);
    ege_fillroundrect(centerX - 172 * scale, centerY - 37 * scale, 350 * scale, 123 * scale, 20 * scale);
    setfillcolor(EGERGB(45, 46, 40));
    ege_fillroundrect(centerX - 159 * scale, centerY - 25 * scale, 327 * scale, 92 * scale, 10 * scale);
    setfillcolor(EGERGB(98, 100, 79));
    ege_fillroundrect(centerX - 133 * scale, centerY - 102 * scale, 277 * scale, 75 * scale, 10 * scale);
    ege_fillrect(centerX - 142 * scale, centerY - 23 * scale, 294 * scale, 81 * scale);
}